package com.aiti.lulian.service.newCard;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.lulian.entity.newCard.CardBaseInfo;
import com.aiti.lulian.mapper.gxmh.GxmhSysMapper;
import com.aiti.lulian.mapper.newCard.CardPersonalBaseInfoMapper;
import com.aiti.lulian.vo.newCard.UserCurrentUseCardInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * description:
 * auth fcb
 * date 2025-08-11 16:43
 **/
@Service
@Slf4j
public class CardPersonalBaseInfoService extends BaseSerivceImpl<CardPersonalBaseInfoMapper, CardBaseInfo> {

    @Autowired
    private GxmhSysMapper gxmhSysMapper;

    /**
     * 用户当前正在使用的名片信息
     * @param username
     * @return
     */
    public UserCurrentUseCardInfoVo getUserCurrentUseCardInfo(String username) {
        String userId = gxmhSysMapper.queryUserIdByUsername(username);
        if(StrUtil.isBlank(userId)) {
            throw new BusinessException("用户不存在");
        }
        UserCurrentUseCardInfoVo userCurrentUseCardInfoVo = this.baseMapper.queryUserCurrentUseCardInfo(userId);
        if(userCurrentUseCardInfoVo == null) {
            throw new BusinessException("未查询到当前用户使用的名片数据");
        }
        LocalDateTime now = LocalDateTime.now();
        if(StrUtil.isNotEmpty(userCurrentUseCardInfoVo.getExpireTime())) {
            LocalDateTime expireTime = DateUtil.parseLocalDateTime(userCurrentUseCardInfoVo.getExpireTime());
            userCurrentUseCardInfoVo.setIsExpire(now.isAfter(expireTime));
        }else {
            userCurrentUseCardInfoVo.setIsExpire(false);
        }
        userCurrentUseCardInfoVo.setUserId(userId);
        return userCurrentUseCardInfoVo;
    }

    /**
     * 用于判断名片是不是当前用户创建的
     * @param userId
     * @param cardId
     * @return
     */
    public Boolean queryUserCardCount(String userId, String cardId) {
        Integer count = this.baseMapper.queryMyCardCount(userId, cardId);
        return count > 0;
    }

    /**
     * 判断名片是否是企业名片
     * @param cardId
     * @return
     */
    public Boolean isEnterpriseCard(String cardId) {
        String enterpriseId = this.baseMapper.selectById(cardId).getEnterpriseId();
        return StrUtil.isNotBlank(enterpriseId);
    }
}
